<?php
namespace app\open\controller;

class DataGame extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
            $date = explodeTime(input('date'));
            $gameId = input('game_id');

            $plat = input('plat');

            if( strtotime(date('Y-m-d', $date['end_time'])) == strtotime(date('Y-m-d', time())) ){
                $selectGame = model('manage/Data')->selectGameNow($plat, $date['start_time'], $date['end_time'], $gameId, '', 'pay_money', session('admin.game_id'));
            }else{
                $selectGame = model('manage/Data')->selectGameOld($plat, $date['start_time'], $date['end_time'], $gameId, '', 'pay_money', session('admin.game_id'));
            }
            if(input('?export')){
                array_unshift($selectGame['data'], ['统计日期', '游戏ID', '游戏名称', '游戏新增', '游戏创角', '游戏活跃', '付费人数', '付费金额', '付费率', '新增用户ARPU', '活跃用户ARPU', '付费用户ARPU']);
                exportExcel($selectGame['data'], input('date').'游戏统计', input('date').'游戏统计');
            }

            $this->layTable(0, '游戏统计', $selectGame['data'], $selectGame['count'], $selectGame['sum']);
        }else{
            $today = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            $date = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            $gameData = model('Base')->gameNotice(session('admin.game_id'));
            return view('', ['date'=>$date, 'today'=>$today, 'game_data'=>$gameData]);
        }
    }

    public function chart(){
        $plat = input('plat');
        $date = explodeTime(input('daytime'));
        $gameId = input('game_id');

        //24小时内
        if( date('Y-m-d', $date['start_time']) == date('Y-m-d', $date['end_time']) ){
            if( date('Y-m-d', $date['end_time'])==date('Y-m-d', time()) ){
                $time = date('H', time())+1;
                for($i=1; $i<=$time; $i++){
                    $d = $i < 10 ? 0 . $i : $i;
                    $dateType[] = $d.':00';
                }
            }else{
                $time = 24;
                $dateType = ['01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00'];
            }
            $findGame = model('manage/Data')->findGameNow($plat, $date['start_time'], $date['end_time'], $gameId, $time, '', session('admin.game_id'));

            //如果小于31天，则按天分组
        }else if( ($date['end_time']-$date['start_time']) < 86400*31 ){
            $findGame = model('manage/Data')->findGameOld($plat, $date['start_time'], $date['end_time'], $gameId, '%m-%d', '', session('admin.game_id'));

            for ($start = $date['start_time']; $start <= $date['end_time']; $start += 24 * 3600) {
                $dateType[] = date('m-d',$start);
            }
            //如果大于31天，按月分组
        }else{
            $findGame = model('manage/Data')->findGameOld($plat, $date['start_time'], $date['end_time'], $gameId, '%Y-%m', '', session('admin.game_id'));

            $i = false;
            $start = $date['start_time'];
            while( $start < strtotime('-1 Month', $date['end_time']) ) {
                $start = !$i ? strtotime('+0 Month', $start) : strtotime('+1 Month', $start);
                $i = true;
                $dateType[] = date('Y-m', $start);
            }
        }

        return view('', ['series'=>$findGame, 'date_type'=>json_encode($dateType)]);
    }
}